<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\AdminBaseController;
use model\GoodsModel;
use think\facade\Db;

class AdminGoodsController extends AdminBaseController
{
    protected $goodsModel;

    public function initialize()
    {
        parent::initialize();
        $this->goodsModel = new  GoodsModel();
    }

    /**
     * 列表
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function index()
    {
        $list = Db::name('goods')->where('is_delete', 0)->paginate(20);

        $goodsList = $list->all();
        foreach ($goodsList as $key=> &$item) {
            $item = $this->goodsModel->decode($item); 

            $list[$key] = $item;
        }

        $this->assign('page', $list->render());//单独提取分页出来
        $this->assign('goods', $list);
        return $this->fetch();
    }


    /**
     * 详情
     */
    public function detail()
    {
        $id = $this->request->get('id');
        if (empty($id)){
            return $this->error('数据错误');
        }
        $goods = $this->goodsModel->where(['id'=>$id])->find();
        $this->assign('goods', $goods);
        return $this->fetch();
    }


    /**
     *  审核
     */
    public function check()
    {
        $id = $this->request->get('id');
        if (empty($id)){
            return $this->error('数据错误');
        }
        $goods = $this->goodsModel->where(['id'=>$id])->find();
        if (!empty($goods)){
            $check = 0;
            if ($goods->check == 0){
                $check = 1;
            }
            $goods->check = $check;
            $goods->save();
        }
        $this->assign('goods', $goods);
        return $this->fetch();
    }


    /**
     *  上下架
     */
    public function status()
    {
        $id = $this->request->get('id');
        if (empty($id)){
            return $this->error('数据错误');
        }
        $goods = $this->goodsModel->where(['id'=>$id])->find();
        if (!empty($goods)){
            $status = 0;
            if ($goods->status == 0){
                $status = 1;
            }
            $goods->status = $status;
            $goods->save();
        }
        $this->assign('goods', $goods);
        return $this->fetch();
    }


    /**
     *  修改商品
     */
    public function editStock()
    {
        $id = $this->request->post('id');
        $stock = $this->request->post('stock');
        if (empty($id)){
            return $this->error('数据错误');
        }
        $stock<=0 and $stock = 0;
        $goods = $this->goodsModel->where(['id'=>$id])->update(['stock'=>$stock]);

        $this->assign('goods', $goods);
        return $this->fetch();
    }


}
